/*
 * Created on Jun 29, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.vectrics.event;

import java.util.Locale;

import org.vectrics.SystemException;
import org.vectrics.common.util.LocalMachineUtils;
import org.vectrics.domain.Domain;
import org.vectrics.mail.EmailHtmlBase;
import org.vectrics.mail.MailMessage;
import org.vectrics.provider.CoreServiceLocator;

/**
 * @author MMoore
 */
public abstract class EventEmailBuilder {
	private Locale locale = null;
	//private Domain domain = null;
	private Event event = null;
	/**
	 * 
	 */
	public EventEmailBuilder(Event event, Locale locale) {
		super();
		this.event = event;
		if (locale == null)
			throw new SystemException("Locale passed to constructor was null and is required");
		this.locale = locale;
	}

	public String getPhrase(String code) {
		String phrase = CoreServiceLocator.getPhraseProvider().getMessage(code, this.getLocale());
		return(phrase);
	}
	
	public String getPhrase(String code, Object[] args) {
		String phrase = CoreServiceLocator.getPhraseProvider().getMessage(code, this.getLocale(), args);
		return(phrase);
	}
	
	public abstract boolean supportsHtml();
	public abstract MailMessage getTextMailMessage();
	public abstract String getSubject();
	protected abstract String getEmailFromName();

	
	protected abstract EmailHtmlBase createHtmlEmail();	

	public Domain getDomain() {
		return(event.getDomain());
	}

	public Event getEvent() {
		return(this.event);
	}
	
	/**
	 * @return Returns the locale.
	 */
	public Locale getLocale() {
		return locale;
	}

	/**
	 * @param locale The locale to set.
	 */
	public void setLocale(Locale locale) {
		this.locale = locale;
	}
	
	public final MailMessage getHtmlMailMessage() {
		MailMessage message = new MailMessage(this.getDomain());
		message.setContentType(MailMessage.CONTENT_HTML);
	
		EmailHtmlBase htmlEmailBase = this.createHtmlEmail();
		
		message.setFromName(this.getEmailFromName());
		
		message.setBody(htmlEmailBase.renderHtml().toString());
		String hostName = LocalMachineUtils.findHostName()
		   + "  /  " + LocalMachineUtils.findHostAddress();
		
		message.setSubject(getSubject() + " -  " + hostName);
		return(message);
	}
	
	
}
